package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.entity.Student;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Author：汤小洋
 * Date：2024-01-29 14:03
 * Description：<描述>
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/index")
public class IndexController {

    private final ElasticsearchRestTemplate restTemplate;

    /**
     * 创建索引
     */
    @RequestMapping("/create")
    public String createIndex() {
        // 创建索引
        IndexOperations indexOperations = restTemplate.indexOps(Student.class);
        indexOperations.create();
        // 创建映射
        Document mapping = indexOperations.createMapping(Student.class);
        indexOperations.putMapping(mapping);

        return "success";
    }

    /**
     * 删除索引
     */
    @RequestMapping("/delete")
    public String deleteIndex() {
        restTemplate.indexOps(Student.class).delete();
        return "success";
    }

}
